package com.lgkj.view;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.lgkj.R;
import com.lgkj.adapter.GoodsModelAdapter;
import com.lgkj.bean.meitao.ComfirOrderBean;
import com.lgkj.bean.meitao.GoodsBean;
import com.lgkj.ui.shop.ComfirOrderActivity;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.CommonUtils;
import com.lgkj.utils.HttpUtils;
import com.lgkj.utils.SharedPreferencesUtils;

import org.json.JSONObject;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/8/23 0023.
 */

public class BottomPopupWindowView extends PopupWindow implements View.OnClickListener {
    private static final String TAG = "BottomPopupWindowView";
    private View mMenuView;
    private List<GoodsBean.DataBean.GoodsInfoBean> data;
    private GoodsModelAdapter adapter;
    ListView listView;
    TextView tv_old_price, tv_price, tv_name;
    ImageView ivImg;
    EditText etNum;
    int stock_num = 999;
    boolean iscar = true;
    private GoodsBean.DataBean.GoodsInfoBean goodsInfoBean;

    public void setData(List<GoodsBean.DataBean.GoodsInfoBean> data) {
        this.data = data;
        adapter.setData(data);
        initData(0);
    }

    public void setIsCar(boolean iscar) {
        this.iscar = iscar;
    }

    private Context context;

    public BottomPopupWindowView(Activity context) {
        super(context);
        this.context = context;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.alert_dialog_goods_details, null);
        adapter = new GoodsModelAdapter(context);
        tv_old_price = mMenuView.findViewById(R.id.tv_old_price);
        tv_price = mMenuView.findViewById(R.id.tv_price);
        tv_name = mMenuView.findViewById(R.id.tv_name);
        ivImg = mMenuView.findViewById(R.id.iv_img);
        etNum = mMenuView.findViewById(R.id.et_num);
        mMenuView.findViewById(R.id.btn_save).setOnClickListener(this);
        mMenuView.findViewById(R.id.iv_colse).setOnClickListener(this);
        mMenuView.findViewById(R.id.iv_jian).setOnClickListener(this);
        mMenuView.findViewById(R.id.iv_add).setOnClickListener(this);
        listView = mMenuView.findViewById(R.id.lv_guige);
        listView.setAdapter(adapter);
        init();
    }

    private void init() {
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.mystyle);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.getBackground().setAlpha(80);
    }


    public void initData(int positoin) {
        stock_num = Integer.parseInt(data.get(positoin).getStock_num());
        Log.i(TAG, "initData: " + adapter.getPosition());
        goodsInfoBean = adapter.getItem(positoin);
        tv_old_price.setText("￥ 原价   " + goodsInfoBean.getOld_price());
        tv_price.setText("￥ 会员价   " + goodsInfoBean.getPrice() + "元");
        tv_name.setText(goodsInfoBean.getModel_describe());
        CommonUtils.LoadImage(context, ivImg, goodsInfoBean.getModel_cover_id(), 0);
    }

    public void setadapter(GoodsModelAdapter.BackCallClick callClick) {
        adapter.setclick(callClick);
    }


    @Override
    public void onClick(View view) {
        int goodsNum = getGoodsNum();
//        if(goodsNum!)
        switch (view.getId()) {
            case R.id.iv_jian:
                if (goodsNum > 1) {
                    goodsNum--;
                } else {
                    Toast.makeText(context, "至少购买一个商品", Toast.LENGTH_SHORT).show();
                }
                etNum.setText(goodsNum + "");
                break;
            case R.id.iv_add:
                if (stock_num <= goodsNum) {
                    Toast.makeText(context, "您买了全部库存商品，不能在买了", Toast.LENGTH_SHORT).show();
                } else {
                    goodsNum++;
                }
                etNum.setText(goodsNum + "");
                break;
            case R.id.btn_save:
                if (iscar) {
                    addCar();
                } else {
                    if (etNum.getText() != null) {
                        addCarGoods();
                    } else {
                        Toast.makeText(context, "至少购买一个商品", Toast.LENGTH_SHORT).show();
                    }
                }
                dismiss();
                break;
            case R.id.iv_colse:
                dismiss();
                break;

        }

    }

    public int getGoodsNum() {
        return Integer.parseInt(etNum.getText().toString());
    }

    private void addCar() {
        CommonUtils.showDiaLog(context, "正在加入购物车");
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(context));
        map.put("goods_id", goodsInfoBean.getGoods_id());
        map.put("goods_model_id", goodsInfoBean.getId());
        map.put("goods_num", getGoodsNum() + "");
        HttpUtils.postHttp(map, CommonConstant.addCart, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                CommonUtils.dismissDiaLog();
                if (jsonObject.optInt("code") == 0) {
                    dismiss();
                    Toast.makeText(context, "加入购物车成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(context, jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void errorHttp(String msg) {
                CommonUtils.dismissDiaLog();
            }
        });

    }

    private void addCarGoods() {
        List<ComfirOrderBean> comfirOrderBean = new ArrayList<>();
        ComfirOrderBean beans = new ComfirOrderBean();
        beans.setGoods_num(Integer.parseInt(etNum.getText().toString()));
        beans.setGoods_price(Double.parseDouble(goodsInfoBean.getPrice()));
        beans.setGoods_name(goodsInfoBean.getGoods_model());
        beans.setCover_id(goodsInfoBean.getModel_cover_id());
        beans.setGoods_model_id(goodsInfoBean.getId());
        beans.setGoods_price1(Double.parseDouble(goodsInfoBean.getOld_price()));
        beans.setGoods_id(goodsInfoBean.getGoods_id());
        beans.setId("-1");
        BigDecimal bDouble = new BigDecimal(beans.getGoods_num());
        BigDecimal bString = new BigDecimal(beans.getGoods_price());
        BigDecimal total = bDouble.multiply(bString);
        Log.i(TAG, "addCarGoods: " + beans.toString());
        beans.setTotalPrice(total);
        comfirOrderBean.add(beans);

        Intent intent = new Intent(context, ComfirOrderActivity.class);
        goodsInfoBean.setGoods_num(getGoodsNum());
        Log.i(TAG, "addCarGoods: " + total);
        intent.putExtra("total", total);
        context.startActivity(intent);
        SharedPreferencesUtils.putBean(context, "carinfo", comfirOrderBean);
    }
}
